Skip to content

fix(storage): honor GOOGLE_CLOUD_UNIVERSE_DOMAIN in the REST client - #19248

Open
shradhakatyal wants to merge 8 commits into
googleapis:mainfrom
shradhakatyal:fix/storage-rest-universe-domain-env
Open

shradhakatyal wants to merge 8 commits into
googleapis:mainfrom
shradhakatyal:fix/storage-rest-universe-domain-env

Conversation

@shradhakatyal

@shradhakatyal shradhakatyal commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

storage.Client now honors the GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable, and the signing methods validate the universe domain of credentials passed for a single call.

Before this change, the REST client read the universe domain only from client_options.universe_domain. A workload that set only the environment variable got a googleapis.com client. GrpcClient and AsyncGrpcClient already read the variable since #19188.

Changes

  • Add _resolve_client_universe() in _helpers.py. It is a local mirror of the shared resolve_client_universe helper and carries the # TODO(universe-domain): header, so it can be replaced by google.api_core.universe.resolve_client_universe later. The local copy also strips surrounding whitespace, because determine_domain in older google-api-core releases returns the value unstripped.
  • Client.__init__ resolves the universe domain with that helper. Precedence is client_options.universe_domain, then the variable, then googleapis.com.
  • Client._universe_domain stays None when the resolved value is the default and client_options did not name it. The default endpoint and mTLS handling are unchanged for that case. The default universe is matched without regard to case, so GOOGLE_CLOUD_UNIVERSE_DOMAIN=GoogleApis.com keeps working.
  • Client.__init__ converts a client_options mapping that is not a dict to a dict, so the constructor accepts every type the resolver accepts.
  • The credential universe check now skips AnonymousCredentials, which carry no token.
  • Add _validate_signing_credentials_universe_domain() in _helpers.py. Blob.generate_signed_url, Bucket.generate_signed_url and Client.generate_signed_post_policy_v4 call it for credentials passed per call.
  • Client.generate_signed_post_policy_v4 passes the client's universe domain when it signs with access_token and service_account_email, as Blob.generate_signed_url does. fix(storage): pass universe_domain in generate_signed_post_policy_v4 #19243 contains the same change, so either merge order works.
  • Add an autouse fixture in tests/unit/conftest.py that clears the variable, so the runner's environment cannot change test results.

Behavior Changes

The first three apply only when GOOGLE_CLOUD_UNIVERSE_DOMAIN is set.

  • Credentials whose universe_domain differs from the variable now raise ValueError at construction. API key credentials are one example, because they report googleapis.com.

  • An empty or whitespace-only value raises EmptyUniverseError (a ValueError), as it does in GrpcClient and the generated clients.

  • A value of googleapis.com changes nothing.

  • An empty or whitespace-only client_options.universe_domain raises EmptyUniverseError. An empty string was ignored before.

  • Surrounding whitespace in client_options.universe_domain is stripped. A padded value raised a credential mismatch ValueError before.

  • client_options that is not a ClientOptions instance, a Mapping, or None raises TypeError.

  • client_options can be any Mapping, for example a types.MappingProxyType. A mapping other than a dict raised AttributeError before.

  • AnonymousCredentials are accepted with any universe domain. Client(client_options={"universe_domain": ...}, credentials=AnonymousCredentials()) raised ValueError before and now works. The same applies to STORAGE_EMULATOR_HOST combined with a non-default universe.

  • Blob.generate_signed_url, Bucket.generate_signed_url and Client.generate_signed_post_policy_v4 raise ValueError when all of these hold:

    1. The per-call credentials sign the result.
    2. They report a non-default universe domain that differs from the client's universe domain.
    3. The result uses the default Storage endpoint of the client's universe domain.

    The default endpoint of a universe domain does not accept such a result, so the call now fails early.

Not Changed

  • A client configured only with client_options.api_endpoint still reports googleapis.com as its universe domain. Set client_options.universe_domain or the environment variable for a non-default universe.
  • Per-call credentials are not compared with the client in these cases:
    • They report googleapis.com. Credentials also report that value when no universe domain was set on them.
    • The caller passes bucket_bound_hostname, or an api_access_endpoint that is not the default endpoint of the client's universe domain. An api_access_endpoint equal to that default endpoint is checked.
    • The client uses a custom endpoint or the emulator.
    • Both access_token and service_account_email are passed, because the credentials object does not sign in that case.
    • They cannot sign. Those calls keep raising AttributeError.

Testing

  • Unit tests for the resolver cover ClientOptions and mapping inputs, an unset variable, the default universe, precedence, surrounding whitespace, empty values, and unsupported client_options types.
  • Unit tests for the per-call credential check cover every branch.
  • Unit tests for the constructor cover matched and mismatched credentials, client_options precedence, empty and padded client_options.universe_domain, the default universe named in client_options or in mixed case in the variable, non-dict mappings, api_endpoint, mTLS, the emulator, and anonymous credentials.
  • Unit tests for the three signing methods cover v2 and v4, virtual-hosted style, explicit endpoints, bucket-bound hostnames, custom client endpoints, a client passed per call, access-token signing, and real service account credentials.
  • Tests with several cases use pytest.mark.parametrize. They are in plain pytest classes, because parametrize is not supported in the unittest.TestCase classes of these files.
  • Verified manually with a local script that sends real HTTP requests to a loopback server, for 28 client and signing configurations.
  • System tests in tests/system/test__signing.py pass universe_domain_credential to the default client for the three signing methods and expect ValueError. They are skipped when TEST_UNIVERSE_DOMAIN_CREDENTIAL is not set, and when the client does not use the default storage endpoint. They need no bucket and make no network call.
  • No system test covers the environment variable itself. That test needs a non-default universe test environment.

storage.Client read the universe domain only from client_options. A
workload that set only GOOGLE_CLOUD_UNIVERSE_DOMAIN got a googleapis.com
client.

Resolve the universe domain from client_options first, then from the
environment variable. Anonymous credentials are now accepted in every
universe, so the emulator and create_anonymous_client keep working when
a non-default universe is configured.

[Generated-by: AI]

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable to configure the universe domain in the Google Cloud Storage client. It adds a helper function to retrieve and validate this override, updates the Client initialization to fall back to this environment variable when universe_domain is not provided in client_options, and updates credential validation to allow AnonymousCredentials in any universe. Comprehensive unit tests and a test fixture to clear the environment variable were also added. The review feedback suggests using mock.patch.dict instead of mock.patch when mocking os.environ in the tests to ensure compatibility and safety.

@shradhakatyal

Copy link
Copy Markdown
Contributor Author

/gcbrun (0407fbe)

…tials

Blob.generate_signed_url, Bucket.generate_signed_url and
Client.generate_signed_post_policy_v4 accept credentials for a single
call. Nothing compared the universe domain of those credentials with the
universe domain of the client. The result could be a signed URL or POST
policy for the default Storage endpoint of one universe domain, signed
by an identity from another. That endpoint does not accept it.

Raise ValueError early when the credentials sign the result, report a
non-default universe domain that differs from the client's, and the
result uses the default Storage endpoint of the client's universe
domain. Credentials that report the default universe domain are not
compared, because credentials also report that value when no universe
domain was set on them. The check is skipped for a custom endpoint, a
bucket-bound hostname or the emulator, and when an access token and a
service account email sign the result.

Client.generate_signed_post_policy_v4 now also passes the client's
universe domain when it signs with an access token and a service account
email, as Blob.generate_signed_url does.

[Generated-by: AI]
Use mock.patch.dict("os.environ", ..., clear=True) in the environment
variable tests this change adds. It keeps os.environ as the standard
mapping object instead of replacing it with a plain dict.

[Generated-by: AI]
@chalmerlowe

Copy link
Copy Markdown
Contributor

@shradhakatyal

As joint maintainers of the various repos in google-cloud-python there are some benefits on aligning on centralized functions, tests, and approaches, where it makes sense.

With the rush to update all the libraries to handle universe-domain, we have a lot of changes being recommended across all of google-cloud-python. To that end, the Cloud Python Team has put together a catalog of functions, environment variables, and such that will be commonly used across the ecosystem. Most of the functions are centralized where possible in google-api-core OR google-cloud-core. These objects are captured in cl/1029785586)**

We noticed #19248 introduces a custom universe resolver (_get_universe_domain_override) that overlaps with the proposed shared helper in catalog.py (cl/1029785586). I recommend the following action:

  • Mirror the function resolve_client_universe from catalog.py (cl/1029785586) locally with a # TODO(universe-domain): comment instead of rolling a custom resolver. the handler in the catalog handles ClientOptions, dict inputs, GOOGLE_CLOUD_UNIVERSE_DOMAIN precedence, and EmptyUniverseError/TypeError validation uniformly, and drops in as a direct google.api_core.universe import once testing/constraints-3.10.txt exceeds 2.44.0.

I know that you have better context on this than I do, so if any of the suggestions above are wrong, please leave a comment here. Gemini will pick them up and learn from them during the next sync sweep.

Note

cl/1029785586) also has a skill that allows Gemini to run a built-in script to check for overlaps in any PR we create with functions in the catalog.py so that we can all spot some of these potential duplicates early.

If you see an issue with an item in the catalog.py, please suggest a change so that we can all benefit.

@shradhakatyal

Copy link
Copy Markdown
Contributor Author

/gcbrun (7aff459)

…universe mirror

Replace `_get_universe_domain_override` with `_resolve_client_universe`, a
local mirror of the shared `resolve_client_universe` helper. The mirror has
the `TODO(universe-domain)` header, so it can be replaced by
`google.api_core.universe.resolve_client_universe` later.

The local copy strips surrounding whitespace, because `determine_domain` in
older google-api-core releases returns the value unstripped.

`Client._universe_domain` stays `None` for the default universe unless
`client_options` names it, so the default endpoint handling is unchanged.

Behavior changes:
- An empty or whitespace-only `client_options.universe_domain` raises
  `EmptyUniverseError`.
- Surrounding whitespace in `client_options.universe_domain` is stripped.
- `client_options` that is not a `ClientOptions`, `Mapping`, or `None`
  raises `TypeError`.

[Generated-by: AI]
@shradhakatyal

Copy link
Copy Markdown
Contributor Author

Done. _get_universe_domain_override is replaced by a local mirror of resolve_client_universe, with the # TODO(universe-domain): header, in _helpers.py.

Two notes on the mirror:

  1. The local copy strips whitespace from the option and the environment value. determine_domain in older google-api-core releases (2.29 and 2.30, and storage tests against 2.28.0) returns the value unstripped, so " example.com " would keep its spaces there. Newer releases strip. The catalog helper may want the same strip until the floor moves.
  2. storage.Client keeps _universe_domain = None when the resolved value is the default and was not set in client_options. The constructor uses that to keep the existing endpoint and mTLS handling for the default universe.

An empty client_options.universe_domain now raises EmptyUniverseError, as GrpcClient does. The PR description lists it under behavior changes.

Co-authored by AI Agent

@shradhakatyal

Copy link
Copy Markdown
Contributor Author

/gcbrun (0f79a76)

@shradhakatyal shradhakatyal added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 1, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 1, 2026
@shradhakatyal
shradhakatyal marked this pull request as ready for review December 1, 2026 07:27
@shradhakatyal
shradhakatyal requested review from a team as code owners December 1, 2026 07:27
Address review comments on the universe domain change.

- `Client.__init__` converts a `client_options` mapping that is not a
  `dict` to a `dict`, so the constructor accepts every type that
  `_resolve_client_universe` accepts.
- `Client.__init__` matches the default universe without regard to case.
  `GOOGLE_CLOUD_UNIVERSE_DOMAIN=GoogleApis.com` keeps the default endpoint.
- The `TODO(universe-domain)` header of `_resolve_client_universe` names
  its tracking bug and issue.
- Tests with several cases use `pytest.mark.parametrize`. They are in plain
  pytest classes, because `unittest.TestCase` does not support it.
- `tests/unit/test_bucket.py` has one signing credentials helper.

[Generated-by: AI]
…iverse

`Blob.generate_signed_url`, `Bucket.generate_signed_url` and
`Client.generate_signed_post_policy_v4` raise `ValueError` when the
per-call credentials report a universe domain that differs from the
client's universe domain.

The tests pass `universe_domain_credential` to the default
`storage_client`. They are skipped when `TEST_UNIVERSE_DOMAIN_CREDENTIAL`
is not set, and when the client does not use the default storage endpoint.

[Generated-by: AI]
@shradhakatyal

Copy link
Copy Markdown
Contributor Author

/gcbrun (77dc85f)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants